package com.example.yiwei.crm.util;

import android.content.Context;
import android.content.res.AssetManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/11.
 */

public class AreaData {
    // 省数据集合
    private ArrayList<String> mListProvince = new ArrayList<String>();
    // 市数据集合
    private ArrayList<ArrayList<String>> mListCiry = new ArrayList<ArrayList<String>>();
    // 区数据集合
    private ArrayList<ArrayList<ArrayList<String>>> mListArea = new ArrayList<ArrayList<ArrayList<String>>>();
    // 省id数据集合
    private ArrayList<String> mListProvinceid = new ArrayList<String>();
    // 市id数据集合
    private ArrayList<ArrayList<String>> mListCiryid = new ArrayList<ArrayList<String>>();
    // 区id数据集合
    private ArrayList<ArrayList<ArrayList<String>>> mListAreaid = new ArrayList<ArrayList<ArrayList<String>>>();
    private JSONObject mJsonObj;
    private String Filename;
    private Context context;
    public AreaData(Context context){
        this.context = context;
        initJsonData(context);
        initJsonDatas();
    }
    public ArrayList<String> getListProvince(){
        return mListProvince;
    }
    public ArrayList<ArrayList<String>> getListCiry(){
        return mListCiry;
    }
    public ArrayList<ArrayList<ArrayList<String>>> getListArea(){
        return mListArea;
    }
    public ArrayList<String> getListProvinceid(){
        return mListProvinceid;
    }
    public ArrayList<ArrayList<String>> getListCiryid(){
        return mListCiryid;
    }
    public ArrayList<ArrayList<ArrayList<String>>> getListAreaid(){
        return mListAreaid;
    }

    /** 从assert文件夹中读取省市区的json文件，然后转化为json对象 */
    private void initJsonData(Context context) {
        Filename = "areajson.json";
        try {
            StringBuffer sb = new StringBuffer();
            AssetManager am = context.getAssets();
            InputStream is = am.open(Filename);
            int len = -1;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len, "UTF-8"));
            }
            is.close();
            mJsonObj = new JSONObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    /** 初始化Json数据，并释放Json对象 */
    private void initJsonDatas(){
        try {
            JSONArray jsonArray = mJsonObj.getJSONArray("areatree");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonP = jsonArray.getJSONObject(i);// 获取每个省的Json对象
                String province = jsonP.getString("text");
                String provinceid = jsonP.getString("id");
                ArrayList<String> options2Items_01 = new ArrayList<String>();
                ArrayList<ArrayList<String>> options3Items_01 = new ArrayList<ArrayList<String>>();
                ArrayList<String> options2Itemsid_01 = new ArrayList<String>();
                ArrayList<ArrayList<String>> options3Itemsid_01 = new ArrayList<ArrayList<String>>();
                JSONArray jsonCs = jsonP.getJSONArray("children");
                for (int j = 0; j < jsonCs.length(); j++) {
                    JSONObject jsonC = jsonCs.getJSONObject(j);// 获取每个市的Json对象
                    String city = jsonC.getString("text");
                    String cityid = jsonC.getString("id");
                    options2Items_01.add(city);// 添加市数据
                    options2Itemsid_01.add(cityid);// 添加市数据
                    ArrayList<String> options3Items_01_01 = new ArrayList<String>();
                    ArrayList<String> options3Itemsid_01_01 = new ArrayList<String>();
                    JSONArray jsonAs = jsonC.getJSONArray("children");
                    for (int k = 0; k < jsonAs.length(); k++) {
                        JSONObject jsonA = jsonAs.getJSONObject(k);
                        String area = jsonA.getString("text");
                        String areaid = jsonA.getString("id");
                        options3Items_01_01.add(area);// 添加区数据
                        options3Itemsid_01_01.add(areaid);// 添加区数据
                    }
                    options3Items_01.add(options3Items_01_01);
                    options3Itemsid_01.add(options3Itemsid_01_01);
                }
                mListProvince.add(province);// 添加省数据
                mListProvinceid.add(provinceid);
                mListCiry.add(options2Items_01);
                mListArea.add(options3Items_01);
                mListCiryid.add(options2Itemsid_01);
                mListAreaid.add(options3Itemsid_01);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mJsonObj = null;
    }
}
